﻿namespace Agent
{
    using System;
    using System.ComponentModel;
    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    public class MaskedTextBox : TextBox
    {
        private MaskedTextProvider a;
        private bool b;
        private bool c;
        private bool d = true;
        private bool e;
        private bool f = true;

        private void a(Key A_0)
        {
            KeyEventArgs input = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, A_0) {
                RoutedEvent = UIElement.KeyDownEvent
            };
            InputManager.Current.ProcessInput(input);
        }

        protected override void OnGotFocus(RoutedEventArgs A_0)
        {
            base.OnGotFocus(A_0);
            if (!this.c)
            {
                this.a(Key.Insert);
                this.c = true;
            }
        }

        protected override void OnLostFocus(RoutedEventArgs A_0)
        {
            base.OnLostFocus(A_0);
            if (this.b != Keyboard.PrimaryDevice.IsKeyToggled(Key.Insert))
            {
                this.a(Key.Insert);
            }
        }

        protected override void OnPreviewKeyDown(KeyEventArgs A_0)
        {
            if (base.SelectionLength > 1)
            {
                base.SelectionLength = 0;
                A_0.Handled = true;
            }
            if (((A_0.Key == Key.Insert) || (A_0.Key == Key.Delete)) || ((A_0.Key == Key.Back) || ((A_0.Key == Key.Space) && this.f)))
            {
                A_0.Handled = true;
            }
            base.OnPreviewKeyDown(A_0);
        }

        protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs A_0)
        {
            if (this.b())
            {
                this.a.Clear();
                this.a.Add(base.Text);
                if (!this.a.MaskFull)
                {
                    A_0.Handled = true;
                }
            }
            base.OnPreviewLostKeyboardFocus(A_0);
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs A_0)
        {
            MaskedTextResultHint hint;
            if (A_0.Text.Length == 1)
            {
                this.e = this.a.VerifyChar(A_0.Text[0], base.CaretIndex, out hint);
            }
            else
            {
                int num;
                this.e = this.a.VerifyString(A_0.Text, out num, out hint);
            }
            base.OnPreviewTextInput(A_0);
        }

        protected override void OnTextInput(TextCompositionEventArgs A_0)
        {
            string text = base.Text;
            if (!this.c())
            {
                A_0.Handled = true;
            }
            else
            {
                base.OnTextInput(A_0);
                if (!this.a.VerifyString(base.Text) || !Regex.Match(base.Text, "^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])?$").Success)
                {
                    base.Text = text;
                }
                while (!this.a.IsEditPosition(base.CaretIndex) && (this.a.Length > base.CaretIndex))
                {
                    base.CaretIndex++;
                }
            }
        }

        public bool IgnoreSpace
        {
            get
            {
                return this.f;
            }
            set
            {
                this.f = value;
            }
        }

        public string Mask
        {
            get
            {
                if (this.a != null)
                {
                    return this.a.Mask;
                }
                return "";
            }
            set
            {
                this.a = new MaskedTextProvider(value);
                base.Text = this.a.ToDisplayString();
            }
        }

        public bool NewTextIsOk
        {
            get
            {
                return this.e;
            }
            set
            {
                this.e = value;
            }
        }

        public bool StayInFocusUntilValid
        {
            get
            {
                return this.d;
            }
            set
            {
                this.d = value;
            }
        }
    }
}

